package main;

import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

import slaverDown.SlaverDown;

import checkStatus.CheckSlaverStatus;
import checkStatus.CheckTaskIdStatus;

import dataStore.updateDataStoreTaskBag.UpdateDataStoreTaskBag;
import dataStruct.DataStoreTask;
import dataStruct.DataStoreTaskBag;
import dataStruct.FtpFileDowndedCount;
import dataStruct.SlaverStatus;

public class MasterMain {

	public static void main(String[] args) throws AlreadyBoundException,
			RemoteException, InterruptedException, SQLException, NotBoundException {
		
		Map<Integer,	 SlaverStatus>  slaverMap = new ConcurrentHashMap<Integer,	 SlaverStatus>();
		
		Queue<DataStoreTask> dataStoreTaskChunkQueue = new LinkedBlockingQueue<DataStoreTask>();
		
		Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap = new ConcurrentHashMap<String, DataStoreTaskBag>();
		Queue<String> dataStoreTaskSlaverChunkQueue = new LinkedBlockingQueue<String>();
		
		Map<String,  FtpFileDowndedCount>  ftpFileDownedSizeMap = new ConcurrentHashMap<String, FtpFileDowndedCount>();
		
		slaverMap.clear();
		dataStoreTaskChunkQueue.clear();
		dataStoreTaskSlaverChunkMap.clear();
		dataStoreTaskSlaverChunkQueue.clear();
		ftpFileDownedSizeMap.clear();
		CreateRegistry.createRegistry(
				slaverMap, 
				dataStoreTaskChunkQueue, 
				dataStoreTaskSlaverChunkQueue, 
				dataStoreTaskSlaverChunkMap,
				 ftpFileDownedSizeMap
				);
		
		/*
		 * check slaver status for move task and data
		 * */
		SlaverDown slaverDown = new SlaverDown(dataStoreTaskSlaverChunkQueue, 
				dataStoreTaskSlaverChunkMap,
				 ftpFileDownedSizeMap
				);
		CheckSlaverStatus checkSlaverStatus = new CheckSlaverStatus(slaverMap, slaverDown);
		checkSlaverStatus.start();
		
		CheckTaskIdStatus checkTaskIdStatus = new CheckTaskIdStatus(dataStoreTaskSlaverChunkQueue,  dataStoreTaskSlaverChunkMap);
		checkTaskIdStatus.start();
		
		UpdateDataStoreTaskBag updateDataStoreTaskBag = new UpdateDataStoreTaskBag(
				dataStoreTaskChunkQueue, 
				dataStoreTaskSlaverChunkQueue,
				dataStoreTaskSlaverChunkMap
				 );
		updateDataStoreTaskBag.start();
	}
}